package com.xmtlxx.common.tengbo;

import com.xmtlxx.common.config.TengboProperties;
import com.xmtlxx.common.tengbo.vo.*;
import com.xmtlxx.common.utils.JsonUtils;
import com.xmtlxx.common.utils.redis.RedisUtils;
import com.xmtlxx.common.wxap.util.HttpInvokerUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 数据同步
 */
@Slf4j
@Component
public class PushDatasUtil {

    private static TengboProperties tengboProperties;
    public final static String QUEUE_KEY = "push:task:list";

    @Autowired
    public void setTengboProperties(TengboProperties tengboProperties){
        PushDatasUtil.tengboProperties = tengboProperties;
    }

    public static void addTask(String key, Object data){
        Queue<TaskItemVo> taskQueuet = RedisUtils.getCacheObject(QUEUE_KEY);
        if(taskQueuet == null){
            taskQueuet = new LinkedList<>();
        }
        TaskItemVo task = new TaskItemVo();
        task.setApi(key);
        key = key+"#"+ new Date().getTime();
        task.setId(key);
        task.setData(data);
        taskQueuet.add(task);
        RedisUtils.setCacheObject(QUEUE_KEY,taskQueuet);
    }

    /**
     * 上架任务登记推送
     * @param data
     * @return
     */
    public static String pushPutTaskDatas(Object data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            PutTaskModelsVo modelsVo = new PutTaskModelsVo();
            modelsVo.setApi("putTask");
            modelsVo.setData(data);
            addTask("putTask",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("pushPutTaskDatas Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 上架任务登记推送
     * @param data
     * @return
     */
    public static String pickTaskDatas(Object data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            PickTaskModelsVo modelsVo = new PickTaskModelsVo();
            modelsVo.setApi("pickTask");
            modelsVo.setData(data);
            addTask("pickTask",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("pushPickTaskDatas Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 接收登记推送
     * @param data
     * @return
     */
    public static String pushStagingDatas(StagingDatasVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            StagingModelsVo modelsVo = new StagingModelsVo();
            modelsVo.setApi("proManage");
            modelsVo.setData(data);
            addTask("proManage",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushRegisterData Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 看样申请反馈
     * @param data
     * @return
     */
    public static String pushViewsDatas(ViewingDatasVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            ViewModelsVo modelsVo = new ViewModelsVo();
            modelsVo.setApi("proManage");
            modelsVo.setData(data);
            addTask("proManage",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushViewsData Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 盘点计划、盘点结果推送
     * @param data
     * @return
     */
    public static String pushInventorysDatas(InventoryPlanDatasVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            PlanModelsVo modelsVo = new PlanModelsVo();
            modelsVo.setApi("inventory");
            modelsVo.setData(data);
            addTask("inventory",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushInventoryPlanData Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 入库单推送
     * @param data
     * @return
     */
    public static String pushInTaskDatas(InTaskDatasVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            InModelsVo modelsVo = new InModelsVo();
            modelsVo.setApi("inWarehouse");
            modelsVo.setData(data);
            addTask("inWarehouse",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushInTaskData Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 出库单推送
     * @param data
     * @return
     */
    public static String pushOutTaskDatas(OutTaskDatasVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            OutModelsVo modelsVo = new OutModelsVo();
            modelsVo.setApi("outWarehouse");
            modelsVo.setData(data);
            addTask("outWarehouse",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushOutTaskData Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 物品记录推送
     * @param data
     * @return
     */
    public static String pushPropertyDatas(PropertyLogModelsVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            addTask("proBase",data);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushPropertyDatas Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 看样申请反馈
     * @param data
     * @return
     */
    public static String pushSuperviseDatas(SuperviseDatasVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            SuperviseModelsVo modelsVo = new SuperviseModelsVo();
            modelsVo.setApi("supervise");
            modelsVo.setData(data);
            addTask("supervise",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushSuperviseData Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 出库单推送
     * @param data
     * @return
     */
    public static String pushHandoverSignsDatas(SignPicsVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            SignPicsModelVo modelsVo = new SignPicsModelVo();
            modelsVo.setApi("handoverSigns");
            modelsVo.setData(data);
            addTask("handoverSigns",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushHandoverSignsDatas Error = >{}",e.getMessage());
        }
        return null;
    }

    /**
     * 处置登记推送
     * @param data
     * @return
     */
    public static String pushDealSignsDatas(RegisterInfoVo data){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            DealRegisterVo modelsVo = new DealRegisterVo();
            modelsVo.setApi("disposalRegistration");
            modelsVo.setData(data);
            addTask("disposalRegistration",modelsVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushDisposalRegistrationDatas Error = >{}",e.getMessage());
        }
        return null;
    }

    public static String pushProManage(List<StagingDatasVo> datas){
        if(!tengboProperties.isEnabled()){ return null; }
        try{
            if(datas != null && datas.size() > 0){
                for(StagingDatasVo data : datas){
                    StagingModelsVo modelsVo = new StagingModelsVo();
                    modelsVo.setApi("proManage");
                    modelsVo.setData(data);
                    log.info("PushProManage Start = >{}", JsonUtils.toJsonString(modelsVo));
                    String result = HttpInvokerUtils.PostToJson(tengboProperties.getUrl(),JsonUtils.toJsonString(data));
                    log.info("PushProManage Over = >{}",result);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushProManage Error = >{}",e.getMessage());
        }
        return null;
    }

    @Scheduled(cron="0 0/3 * * * ?")
    public void taskSchedule(){
        Queue<TaskItemVo> taskQueue = RedisUtils.getCacheObject(QUEUE_KEY);
        Queue<TaskItemVo> newTaskQueue = new LinkedList<>();
        RedisUtils.setCacheObject(QUEUE_KEY,newTaskQueue);
        log.info("taskSchedule TaskList = >{}", JsonUtils.toJsonString(taskQueue));
        if(taskQueue != null){
            while(!taskQueue.isEmpty()){
                TaskItemVo item = taskQueue.poll();
                try{
                    if(item.getApi().equals("proBase")){
                        log.info("PushData Start = >{}", JsonUtils.toJsonString(item.getData()));
                        String result = HttpInvokerUtils.PostToJson(tengboProperties.getUrl(),JsonUtils.toJsonString(item.getData()));
                        log.info("PushData Over = >{}",result);
                        JSONObject object = JSONObject.fromObject(result);
                        if(object.getInt("code") != 200){
                            addTask(item.getApi(),item.getData());
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    log.error("PushData Error = >{}",e.getMessage());
                    addTask(item.getApi(),item.getData());
                }
            }
        }

    }

}
